package com.coocaa.ops.common.tool.md5;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author bijiahao
 * @date : 2019/6/24.
 * @description
 */
public class MD5Util {
    private MD5Util() {
    }

    public static String md5(String plainText) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");

        try {
            md.update(plainText.getBytes("utf-8"));
        } catch (UnsupportedEncodingException var6) {
            var6.printStackTrace();
        }

        byte[] b = md.digest();
        StringBuffer buf = new StringBuffer("");

        for(int offset = 0; offset < b.length; ++offset) {
            int i = b[offset];
            if (i < 0) {
                i += 256;
            }

            if (i < 16) {
                buf.append("0");
            }

            buf.append(Integer.toHexString(i));
        }

        return buf.toString().toLowerCase();
    }

    public static void main(String[] args) throws Exception{
        String originPassword = "admin";
        String salt = "123456";
        String md5Password = MD5Util.md5(MD5Util.md5(originPassword + salt));
        System.out.println(md5Password);
    }
}
